
#ifndef _RECORDMANAGER_H_20111127
#define _RECORDMANAGER_H_20111127

#include <string>
#include <set>

namespace AirlineRes
{

/// Class to store and manage all Records of a particular type, e.g. customer, flight.
template <class RecordType>
class RecordManager
{
public:
  /// Comparator used internally by the RecordSet set to determine placement of records.
  class RecordSetComparator
  {
  public:
    bool operator()(RecordType const& _a, RecordType const& _b ) { return _a.GetID() < _b.GetID(); };
  };

  /// A std::set of RecordType Records.
  typedef std::set< RecordType, RecordSetComparator > RecordSet;

  RecordManager() : m_maxID( -1 ) {};
  ~RecordManager(){};

  // TODO: Return type.

  /// Open a file and reads in data as records of RecordType, storing them in the RecordSet.
  ///
  /// @param _filePath  Full or relative file path of the file to read from.
  ///
  /// @return  An error code represting whether or not the operation was a success.
  int LoadRecords( std::string const& _filePath );

  /// Open a file and write all records stored this RecordManager to it.
  ///
  /// @param _filePath  Full or relative file path of the file to write to.
  ///
  /// @return  An error code represting whether or not the operation was a success.
  int WriteRecords( std::string const& _filePath );

  /// Prints every record in the RecordSet to the specified stream.
  ///
  /// @param _stream  An ostream object to write to.
  ///
  /// @return  An error code represting whether or not the operation was a success.
  int PrintRecords( std::ostream& _stream );

  /// Get a pointer to the RecordSet for the purpose of querying for records.
  ///
  /// @return  Pointer to the RecordSet.
  RecordSet const* const GetRecordSet() const { return &m_records; };

  /// Get a specific Record, given its ID.
  ///
  /// @param _id  ID of the record.
  ///
  /// @return  Pointer to the Record or a null pointer if no such record exists.
  RecordType const* GetRecord( int _id ) const;

  /// Adds a record to the record set. Automatically sets an appropriate ID value for the record.
  ///
  /// @param _record  Record object to add.
  ///
  /// @return  An error code represting whether or not the operation was a success.
  int AddRecord( RecordType const& _record );

  /// Modifies the data of a pre-existing record, the record's ID remains constant.
  ///
  /// @param _record  A Record object containing modified data but with the same ID as the old record.
  ///
  /// @return  An error code represting whether or not the operation was a success.
  int EditRecord( RecordType const& _record );

  /// Deletes a record from the record set.
  ///
  /// @param _id  ID of the record to delete.
  ///
  /// @return  An error code represting whether or not the operation was a success.
  int DeleteRecord( int _id );

private:
  /// Internally used book-keeping function for determining the next unique ID to use when AddRecord is called.
  void UpdateMaxID();

  int m_maxID;
  RecordSet m_records;
};

} // namespace AirlineRes

#include "inl/RecordManager.inl"

#endif // _RECORDMANAGER_H_20111127
